/*
 * SingleWayLayout.java
 * business
 *
 * Created by ChenTao on 2017/2/21.
 *
 * Copyright (c) 2017年 yidiandao. All rights reserved.
 */

package com.yidiandao.business.ui.widget.view;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;

import com.yidiandao.business.utils.UIUtils;

import java.util.List;

/** 确认订单页面-支付方式/配送方式对应的一个组 */
public class SingleWayLayout extends LinearLayout implements View.OnClickListener {

    private Context mContext;

    private OnLayoutItemClickListener mListener;

    private List<String> mData;

    private int mCurrentPosition;

    public SingleWayLayout(Context context) {
        this(context, null);
    }

    public SingleWayLayout(Context context, AttributeSet attrs) {
        super(context, attrs);

        mContext = context;

        // 方向为竖直方向
        setOrientation(LinearLayout.VERTICAL);
    }

    public int getLayoutHeight() {
        int heightDp = 1 + 34 * getChildCount();
        return UIUtils.dp2px(heightDp);
    }

    public List<String> getData() {
        return mData;
    }

    public void setData(List<String> data) {
        removeAllViews();

        mData = data;

        for (int i = 0; i < data.size(); i++) {

            SingleWayItem item = new SingleWayItem(mContext);
            item.setTitle(data.get(i));
            item.setChecked(false);

            if (i == 0) {
                item.setFirstVisible();
            }

            if (i == data.size() - 1) {
                item.setLastVisible();
            }

            // id 即为 position
            item.setId(i);
            item.setOnClickListener(this);

            addView(item);
        }

        requestLayout();
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();

        for (int i = 0; i < getChildCount(); i++) {
            View child = getChildAt(i);
            if (!(child instanceof SingleWayItem)) {
                throw new IllegalArgumentException("child view must be SingleWayItem");
            }
        }
    }

    public int getItemCount() {
        return getChildCount();
    }

    @Override
    public void onClick(View v) {

        int id = v.getId();

        mCurrentPosition = id;

        for (int i = 0; i < getChildCount(); i++) {
            ((SingleWayItem) getChildAt(i)).setChecked(id == i);
        }

        if (mListener != null) {
            mListener.onLayoutItemClick(mCurrentPosition);
        }
    }

    /** 获取当前的选项 */
    public int getCurrentPosition() {
        return mCurrentPosition;
    }

    /** 设置选中 position */
    public void setCurrentPosition(int position) {
        mCurrentPosition = position;

        for (int i = 0; i < getChildCount(); i++) {
            ((SingleWayItem) getChildAt(i)).setChecked(position == i);
        }
    }

    public void setOnLayoutItemClickListener(OnLayoutItemClickListener listener) {
        mListener = listener;
    }

    public interface OnLayoutItemClickListener {
        void onLayoutItemClick(int position);
    }
}
